package com.google.donutrush;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Entities.RTentry;
import Entities.User;
import Services.Mailman;
import Services.UserService;
import Services.UserServiceDBimpl;

public class RegisterServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response){
		//we use this class instance to insert the user and the score to a DB
		UserService us = UserServiceDBimpl.getInstance();
		int SignLevel = (Integer) request.getAttribute("SignLevel");
		int loggedIn = (Integer) request.getAttribute("loggedIn");
		int faceboookUser = (Integer) request.getAttribute("facebookUser");
		
		boolean isRegSuccesfull = false;
		User newuser = (User) request.getAttribute("newUser");
		RTentry rtEntry = (RTentry) request.getAttribute("newRTentry");


		/////case user is already logged in
		if(loggedIn==1){
			if(SignLevel == 1){
				//update user score
				us.registerScore(rtEntry);
				us.updateUserScore(newuser);
				
				
				
			}
			else if(SignLevel == 0){
				//do nothing to DB's
			}
			
			
		}
		
		
		
		
		/////user not logged in
		else{
			
			if(SignLevel == 2){
				
				//first register new user to user DB, maybe with errors
				if (!(newuser.getName().equals("")) && !(newuser.getEmail().equals("")) && !(newuser.getPassword().equals(""))){
					isRegSuccesfull = us.registerUser(newuser);
				}
				
				if(!isRegSuccesfull){
					ServletContext context = getServletContext();
					context.setAttribute("error", "Register failed, either user name or e-mail are already taken!");
					RequestDispatcher rd = request.getRequestDispatcher("Error.do");
					try {
						rd.forward(request, response);
					} catch (ServletException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else{
				//next, insert user score to RT
				us.registerScore(rtEntry);
				//send ver email
				if (!newuser.isFacebookUser()){
					Mailman.sendVerMail("zzzoshri@gmail.com", newuser.getEmail(), newuser.getName(), newuser.getEmail(), newuser.getPassword());
					}
				}
			}
			else if(SignLevel == 1){
				//write to RT only
				us.registerScore(rtEntry);
			}
			else if(SignLevel == 0){
				//do nothing
			}
		}
		
		if(isRegSuccesfull || SignLevel<2 || loggedIn==1){
			//next servlet will handle query from DB, so we pass on the req/res
			RequestDispatcher rd = request.getRequestDispatcher("View.do");
				try {
					rd.forward(request, response);
				} catch (ServletException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
			
			
	}
}
